package main.validator;

import java.util.HashMap;
import java.util.Map;

import main.validator.impl.AgeRangeValidator;
import main.validator.impl.CustomValidator;
import main.validator.impl.DateRangeValidator;
import main.validator.impl.GtValidator;
import main.validator.impl.IdCardUnionValidator;
import main.validator.impl.IdCardValidator;
import main.validator.impl.LengthValidator;
import main.validator.impl.LtValidator;
import main.validator.impl.MaxSizeValidator;
import main.validator.impl.MinSizeValidator;
import main.validator.impl.RegxValidator;
import main.validator.impl.RequiredValidator;
import main.validator.impl.SumValidator;



/**
 * 校验方法工厂
 */
public class ValidatorFactory {
	
	private static Map<String,Validator> validatorPool = null;
	
	static {
		validatorPool = new HashMap<String, Validator>();
		//必填项
		validatorPool.put("required", new RequiredValidator());
		//最小长度
		validatorPool.put("minSize", new MinSizeValidator());
		//最大长度
		validatorPool.put("maxSize", new MaxSizeValidator());
		//正则校验
		validatorPool.put("regx", new RegxValidator());
		//年龄范围
		validatorPool.put("ageRange", new AgeRangeValidator());
		//身份证
		validatorPool.put("idCard", new IdCardValidator());
		//身份证
		validatorPool.put("idCardUnion", new IdCardUnionValidator());
		//字符长度
		validatorPool.put("length", new LengthValidator());
		//加和
		validatorPool.put("sum", new SumValidator());
		//大于某个整数
		validatorPool.put("gt", new GtValidator());
		//小于某个整数
		validatorPool.put("lt", new LtValidator());
		//日期范围（零时）
		validatorPool.put("dateRange", new DateRangeValidator());
	}
	
	public static Validator createValidator(String validatorName){
		if(null != validatorName){
			return validatorPool.get(validatorName.trim());
		}
		else{
			return null;
		}
	}
	
	/**
	 * 对于自定义校验方法比较特殊
	 * @param s 第三方校验
	 * @return
	 */
	public static Validator createCustomValidator(Validator s ){
		return new CustomValidator(s);
	}
}